package com.wl156.adapters;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wl156.R;

public class TruckMessageAdapter extends CheckableAdapter implements
		OnClickListener {

	public TruckMessageAdapter(Context context, SoapObject data) {
		mInflater = LayoutInflater.from(context);
		mData = data;
		mContext = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_truck_message, null);
		}

		TextView from_to = (TextView) convertView.findViewById(R.id.from_to);
		TextView carCode = (TextView) convertView.findViewById(R.id.carCode);
		TextView xinyongscore = (TextView) convertView
				.findViewById(R.id.xinyongscore);

		TextView truck_type_weight_length = (TextView) convertView
				.findViewById(R.id.truck_type_weight_length);
		TextView driver = (TextView) convertView.findViewById(R.id.driver);
		TextView setOffDate = (TextView) convertView
				.findViewById(R.id.set_off_date);

		TextView phoneDial = (TextView) convertView.findViewById(R.id.dial);
		phoneDial.setOnClickListener(this);

		from_to.setText(String.format("%s=>%s",
				getTextViewStringFromProperty(position, 1),
				getTextViewStringFromProperty(position, 2)));
		setTextViewStringFromProperty(position, carCode, 11);
		setTextViewStringFromProperty(position, xinyongscore, 10);

		truck_type_weight_length.setText(String.format("%s %s %s",
				getTextViewStringFromProperty(position, 3),
				getTextViewStringFromProperty(position, 4),
				getTextViewStringFromProperty(position, 5)));
		setTextViewStringFromProperty(position, driver, 7);
		setTextViewStringFromProperty(position, setOffDate, 6);

		setPhoneView(position, phoneDial, 8);

		super.getView(position, convertView, parent);

		if (position % 2 != 0) {
			convertView.setBackgroundColor(Color.parseColor("#F0FAFE"));
		} else {
			convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
		}

		return convertView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dial:
			Intent i = new Intent(Intent.ACTION_CALL);
			i.setData(Uri.parse("tel:" + v.getTag().toString()));
			mContext.startActivity(i);
			break;
		}
	}
}
